package com.travel.hadler;

import com.travel.common.Result;
import com.travel.exception.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        return Result.error("Server error: " + e.getMessage());
    }

    /**
     * 处理未认证异常
     */
    @ExceptionHandler(UnauthorizedException.class)
    public ResponseEntity<Result<?>> handleUnauthorizedException(UnauthorizedException e) {

        return ResponseEntity
                .status(HttpStatus.UNAUTHORIZED) // 设置HTTP状态码401
                .body(Result.AuthError(e.getMessage(), 401)); // 返回统一结果
    }

}
